package com.ffapp.demo.kingbasedbhibernate.controller;

import com.ffapp.demo.kingbasedbhibernate.entiy.User;
import com.ffapp.demo.kingbasedbhibernate.service.UserService;
import com.ffapp.demo.kingbasedbhibernate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {
    @Autowired
    UserService userService;

    /**
     * 保存user
     * @param user
     * @return
     */
    @PostMapping("/saveUser")
    public Result saveUser(@RequestBody User user){
        return userService.saveUser(user);
    }

    /**
     * 查询所有user
     * @return
     */
    @GetMapping("/queryAll")
    public Result queryAll(){
        System.out.println("-----------------");
        List<User> userList=  userService.queryAll();
        for (User user:userList) {
            System.out.println(user.toString()+"\n");
        }

        return null;
    }

    /**
     * 通过name和age查询
     * @param name
     * @param age
     * @return
     */
    @GetMapping("/getByNameAndAge")
    public Result getByNameAndAge(String name,String age){
        return userService.getByNameAndAge(name,age);
    }
}